package com.dongnam.quanly.web;

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dongnam.quanly.common.ActionCategory;
import com.dongnam.quanly.common.ActionType;
import com.dongnam.quanly.common.ObjectType;
import com.dongnam.quanly.common.ScreenId;
import com.dongnam.quanly.common.controller.ConsoleUserController;
import com.dongnam.quanly.common.model.ConsoleUser;
import com.dongnam.quanly.common.util.CryptUtil;
import com.dongnam.quanly.web.audit.AuditLogger;
import com.dongnam.quanly.web.util.LocaleMessages;

@ManagedBean(name = "ChangePasswordBean")
@ViewScoped
public class ChangePasswordBean implements Serializable {

	private static final long serialVersionUID = 1L;

	private static final Logger logger = LoggerFactory.getLogger(ChangePasswordBean.class);

	@ManagedProperty(value = "#{UserBean}")
	private UserBean userBean;

	private String oldPassword;
	private String newPassword;
	private String confirmPassword;

	// ////////// Actions ////////////

	public void savePassword() {
		logger.debug("save passsword");

		// find user
		ConsoleUser user = new ConsoleUserController().findById(userBean.getUser().getUserId(), true);
		if (user.getPassword().equals(encrypt(oldPassword))) {
			if (newPassword.equals(confirmPassword)) { // if newPassword and
														// confirmPassword match
				user.setPassword(encrypt(newPassword));
				new ConsoleUserController().makePersistent(user);
				FacesMessage message = LocaleMessages.getMessage("general_user_password_changeOK");
				message.setSeverity(FacesMessage.SEVERITY_INFO);
				FacesContext.getCurrentInstance().addMessage(null, message);
				AuditLogger.logAuditTrail(ScreenId.CHANGEPASSWORD, ActionCategory.MISC, ActionType.UPDATE, true,
						ObjectType.User, user.getUserId(), null, "Old Password: " + encrypt(oldPassword),
						"New Password: " + encrypt(newPassword));
			} else {
				// newPassword and confirmPassword dont match
				FacesMessage message = LocaleMessages.getMessage("general_user_password_changeConfirmNotMatch");
				message.setSeverity(FacesMessage.SEVERITY_ERROR);
				FacesContext.getCurrentInstance().addMessage(null, message);
				AuditLogger.logAuditTrail(ScreenId.CHANGEPASSWORD, ActionCategory.MISC, ActionType.UPDATE, false,
						ObjectType.User, user.getUserId(), "password [confirm not match]", null, null);
			}
		} else {
			// oldPassword is incorrect
			FacesMessage message = LocaleMessages.getMessage("general_user_password_changeNotMatch");
			message.setSeverity(FacesMessage.SEVERITY_ERROR);
			FacesContext.getCurrentInstance().addMessage(null, message);
			AuditLogger.logAuditTrail(ScreenId.CHANGEPASSWORD, ActionCategory.MISC, ActionType.UPDATE, false,
					ObjectType.User, user.getUserId(), "update password [wrong password]", null, null);
		}
		oldPassword = "";
		newPassword = "";
		confirmPassword = "";
	}

	// ////////// Privates ////////////

	// private String decrypt(String password) {
	// try {
	// return CryptUtil.decrypt(password);
	// } catch (Exception e) {
	// e.printStackTrace();
	// return null;
	// }
	// }

	private String encrypt(String password) {
		try {
			return CryptUtil.encrypt(password);
		} catch (Exception e) {
			logger.error("Error during encrpty", e);
			return null;
		}
	}

	/*** getters / setters ***/
	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

}
